//
//  HJInviteGuestSelectVisitationVC.m
//  Keping
//
//  Created by apple on 2017/11/8.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "HJInviteGuestSelectVisitationVC.h"
#import "HJVisitFriendInfoController.h"

@interface HJInviteGuestSelectVisitationVC ()<UITableViewDelegate, UITableViewDataSource, HJSelectDateViewDelegate>

/**
 tableView
 */
@property(nonatomic,weak)UITableView*tableView;
/**
 数据源
 */
@property(nonatomic,strong)NSMutableArray*dataAry;

@end

@implementation HJInviteGuestSelectVisitationVC

-(NSMutableArray *)dataAry
{
    if (!_dataAry) {
        _dataAry=[NSMutableArray arrayWithObjects:@[@"Select Visitors"],@[@"Enter Guest Detail Manually"], nil];
    }
    return _dataAry;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self getData];
}
-(void)setUI
{
    self.navigationItem.title=@"Invite Guests";
    UITableView*tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.backgroundColor=[UIColor colorWithHexString:@"#f2f1f6"];
    [self.view addSubview:tableView];
    self.tableView=tableView;
    
    //设置头部
    HJHeaderImgView*headerView=[HJHeaderImgView headerImgView:KAppointmentTypeForInvite];
    tableView.tableHeaderView=headerView;
    
    //添加table尾部
    UIView*footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, K_FactorH(80))];
    tableView.tableFooterView=footerView;
    footerView.backgroundColor=[UIColor clearColor];
    CGFloat margin =K_FactorW(20);
    UIButton*addBtn=[[UIButton alloc]initWithFrame:CGRectMake(margin, K_FactorH(30), footerView.width-2*margin, footerView.height-K_FactorH(30))];
    [footerView addSubview:addBtn];
    addBtn.layer.cornerRadius=addBtn.height/2;
    addBtn.layer.masksToBounds=YES;
    [addBtn setBackgroundColor:[UIColor colorWithHexString:@"#6853fa"] forState:UIControlStateNormal];
    [addBtn setTitle:@"Done" forState:UIControlStateNormal];
    addBtn.titleLabel.font=[UIFont boldSystemFontOfSize:20];
    [addBtn addTarget:self action:@selector(clickNextStep) forControlEvents:UIControlEventTouchUpInside];
}

- (void)clickNextStep {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"KPushAppOintmentsForGuest" object:nil];
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}

#pragma mark --UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataAry.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataAry[section] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    HJVisitFriendViewCell*cell=[HJVisitFriendViewCell cellWithTableView:tableView];
    id friend = self.dataAry[indexPath.section][indexPath.row];
    if ([friend isKindOfClass:[NSString class]]) {
        cell.textLabel.text = friend;
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        if (indexPath.section == 1) {
            cell.detailTextLabel.numberOfLines = 0;
            cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
            cell.detailTextLabel.text = @"For a guest who is not in your friend list, you can enter their basic information manually. Invitation can be sent via SMS, WeChat and WhatsApp.";//@"Basic information manually: Basic information manually\ncan be sent via: can be sent via\nWhatsApp : WhatsApp";
        }
    } else {
        RelationFriendViews *relation = friend;
        cell.textLabel.text = relation.nickName;
    }
    if (section == 0) {
        cell.isEdit = NO;
    } else {
        cell.isEdit = NO;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (section == 0) {
        HJInviteSelectVisitationVC *vc = [[HJInviteSelectVisitationVC alloc] init];
        vc.inviteEntityID = self.inviteEntityID;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        HJVisitFriendInfoController *visit = [[HJVisitFriendInfoController alloc] init];
        visit.preType = PreAppointmentTypeForInvte;
        visit.date = self.date;
        visit.time = self.time;
        visit.relationFriendID = self.inviteEntityID;
        [self.navigationController pushViewController:visit animated:YES];
    }
}
-(void)creatDatePicker:(UIDatePickerMode)dateModel
{
    HJSelectDateView*dateView=[[HJSelectDateView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, APP_Frame_Height)];
    dateView.delegate=self;
    dateView.DateModel=dateModel;
    [self.navigationController.view addSubview:dateView];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return K_FactorH(100);
    }
    return K_FactorH(60);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return K_FactorH(10);
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

#pragma mark -- HJSelectDateViewDelegate
-(void)selectDateView:(HJSelectDateView*)selectDateView clickSelectDate:(NSString*)dateStr indexPath:(NSIndexPath*)indexPath {
    NSMutableArray *arr = [self.dataAry[1] mutableCopy];
    if (selectDateView.DateModel == UIDatePickerModeDate) {
        [arr replaceObjectAtIndex:0 withObject:dateStr];
    } else {
        [arr replaceObjectAtIndex:1 withObject:dateStr];
    }
    [self.dataAry replaceObjectAtIndex:1 withObject:arr];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
